package cn.ht.jywrwater.module.ht.service.customermeter;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.customermeter.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.customermeter.CustomerMeterDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.customermeter.CustomerMeterConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.customermeter.CustomerMeterMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水表用户绑定关系 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class CustomerMeterServiceImpl implements CustomerMeterService {

    @Resource
    private CustomerMeterMapper customerMeterMapper;

    @Override
    public Long createCustomerMeter(CustomerMeterCreateReqVO createReqVO) {
        // 插入
        CustomerMeterDO customerMeter = CustomerMeterConvert.INSTANCE.convert(createReqVO);
        customerMeterMapper.insert(customerMeter);
        // 返回
        return customerMeter.getId();
    }

    @Override
    public void updateCustomerMeter(CustomerMeterUpdateReqVO updateReqVO) {
        // 校验存在
        validateCustomerMeterExists(updateReqVO.getId());
        // 更新
        CustomerMeterDO updateObj = CustomerMeterConvert.INSTANCE.convert(updateReqVO);
        customerMeterMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerMeter(Long id) {
        // 校验存在
        validateCustomerMeterExists(id);
        // 删除
        customerMeterMapper.deleteById(id);
    }

    private void validateCustomerMeterExists(Long id) {
        if (customerMeterMapper.selectById(id) == null) {
            throw exception(CUSTOMER_METER_NOT_EXISTS);
        }
    }

    @Override
    public CustomerMeterDO getCustomerMeter(Long id) {
        return customerMeterMapper.selectById(id);
    }

    @Override
    public List<CustomerMeterDO> getCustomerMeterList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return customerMeterMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CustomerMeterDO> getCustomerMeterPage(CustomerMeterPageReqVO pageReqVO) {
        return customerMeterMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CustomerMeterDO> getCustomerMeterList(CustomerMeterExportReqVO exportReqVO) {
        return customerMeterMapper.selectList(exportReqVO);
    }

}
